package com.example.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.standard.StandardPageDto;
import com.example.entity.Standard;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 数据标准目录表 Mapper 接口
 * </p>
 *
 * @author zhj
 * @since 2023-03-23
 */
@Mapper
@Repository
public interface StandardMapper extends BaseMapper<Standard> {

    default Page<Standard> queryStandardPage(StandardPageDto standardPageDto) {
        LambdaQueryWrapper<Standard> queryWrapper = Wrappers.<Standard>lambdaQuery();
        //参数为空的场景下需要考虑值是否传递到sql中查询
        if(StringUtils.isNotBlank(standardPageDto.getStdSource())){
            queryWrapper.like(Standard::getStdSource,standardPageDto.getStdSource());
        }
        if(StringUtils.isNotBlank(standardPageDto.getStdCode())){
            queryWrapper.like(Standard::getStdCode,standardPageDto.getStdCode());
        }
        if(StringUtils.isNotBlank(standardPageDto.getStdChinese())){
            queryWrapper.like(Standard::getStdChinese,standardPageDto.getStdChinese());
        }
        if(StringUtils.isNotBlank(standardPageDto.getStdEnglish())) {
            queryWrapper.like(Standard::getStdEnglish,standardPageDto.getStdEnglish());
        }
        if(standardPageDto.getStatus() != null){
            queryWrapper.eq(Standard::getStatus,standardPageDto.getStatus());
        }
        //分页查询必须要给一个Ipage实现类 才会被拦截器PaginationInnerInterceptor拦截到
        Page<Standard> queryPage = new Page<>(standardPageDto.getCurrentPage(),standardPageDto.getPageSize());
        return this.selectPage(queryPage,queryWrapper);
    }

    Standard selectByChinese(@Param("stdChinese") String stdChinese);

    Standard selectByEnglish(@Param("stdEnglish") String stdEnglish);

    @Select("select * from standard order by id desc limit 1")
    Standard queryMaxIdStandard();
}
